package dang.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

import dang.dao.UserDao;
import dang.entity.User;
import dang.exception.DangException;

public class UserDaoImpl   implements UserDao {
	private Connection connection;

	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	@Override
	public void save(User user) throws DangException {
		try{
			String sql = "insert into d_user (email,nickname,password) values(?,?,?)";
			PreparedStatement pst = connection.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
			// 执行插入操作
			pst.setString(1, user.getEmail());
			pst.setString(2, user.getNickname());
			pst.setString(3, user.getPassword());
			pst.execute();
			//获取自动生成的id,赋值给user  user.setId(dd);
			ResultSet rs=pst.getGeneratedKeys();
			int id=-1;
			if(rs.next()){
				id=rs.getInt(1);
			}
			user.setId(id);
		}catch(Exception e){
			e.printStackTrace();
			throw new DangException(e);
		}
	}

	@Override
	public void updateVerifyState(User user) throws DangException {
		try {
			String sql="update d_user set is_email_verify=?,email_verify_code=? where id=?";
			PreparedStatement pst=connection.prepareStatement(sql);
			String verified="no";
			if(user.getIs_email_verify()==1){
				verified="yes";
			}
			pst.setString(1, verified);
			pst.setString(2, user.getEmail_verify_code());
			pst.setInt(3, user.getId());
			pst.execute();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DangException(e);
		}
	}


	@Override
	public User findById(int id) throws Exception {
		String sql="select * from d_user where id=?";
		PreparedStatement pst=connection.prepareStatement(sql);
		pst.setInt(1, id);
		ResultSet rs=pst.executeQuery();
		User u =new User();
		u.setId(id);
		if(rs.next()){
			u.setEmail_verify_code(rs.getString("email_verify_code"));
			u.setLast_login_ip(rs.getString("last_login_ip"));
			u.setLast_login_time(rs.getLong("last_login_time"));
			u.setNickname(rs.getString("nickname"));
		}
		return u;
	}

	@Override
	public String verifyEmail(String email) throws Exception {
		String sql ="select id from d_user where email=?";
		System.out.println(connection);
		PreparedStatement pst = connection.prepareStatement(sql);
		pst.setString(1, email);
		ResultSet rs = pst.executeQuery();
		if(rs.next()){
			return "emailExisted";
		}
		return "emailKeyong";
	}


	@Override
	public void updateLoginState(User user) throws DangException {
		String sql="update d_user set last_login_ip=?,last_login_time=? where email=?";
		try {
			PreparedStatement ps = connection.prepareStatement(sql);
			ps.setString(1, user.getLast_login_ip());
			ps.setLong(2, user.getLast_login_time());
			ps.setString(3, user.getEmail());
			ps.execute();
		} catch (Exception e) {
			e.printStackTrace();
			throw new DangException(e);
		}
	}

	@Override
	public User findByEmail(String email) throws Exception {
		String sql ="select * from d_user where email=?";
		PreparedStatement pst = connection.prepareStatement(sql);
		pst.setString(1, email);
		ResultSet rs = pst.executeQuery();
		User user=null;
		if(rs.next()){
			user=new User();
			user.setId(rs.getInt("id"));
			user.setEmail_verify_code(rs.getString("email_verify_code"));
			user.setIs_email_verify(rs.getString("is_email_verify").equals("yes")?1:0);
			user.setLast_login_ip(rs.getString("last_login_ip"));
			user.setLast_login_time(rs.getLong("last_login_time"));
			user.setNickname(rs.getString("nickname"));
			user.setPassword(rs.getString("password"));
		}
		return user;
	}

}
